package com.yanqu.road.server.cmd.activity.ghost;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.pb.activity.GhostActivityProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.ghost.GhostMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_GHOST_DAMAGE_RANK_LIST, desc = "获取鬼市怪物伤害榜单")
public class GetDamageRankListCmd extends AbstractCommand {

    private int bossId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        GhostActivityProto.GetGhostDamageRankReqMsg reqMsg = GhostActivityProto.GetGhostDamageRankReqMsg.parseFrom(packet.getBytes());
        bossId = reqMsg.getBossId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ActivityInfo activityInfo = GhostMgr.getActivityInfo();
        if (!ActivityMgr.activityInShowTime(activityInfo)) {
            ret = GameErrorCode.E_GHOST_ACTIVITY_NO_OPEN;
            return;
        }

        GhostActivityProto.CrossGetGhostDamageRankReqMsg.Builder builder = GhostActivityProto.CrossGetGhostDamageRankReqMsg.newBuilder();
        builder.setActivityId(activityInfo.getActivityId());
        builder.setServerId(player.getUserInfo().getOriginalServerId());
        builder.setBossId(bossId);
        player.sendPacket(CrossProtocol.C2_GHOST_ACTIVITY_DAMAGE_RANK_LIST, builder);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            GhostActivityProto.GetGhostDamageRankRespMsg.Builder builder = GhostActivityProto.GetGhostDamageRankRespMsg.newBuilder();
            builder.setRet(ret);
            player.sendPacket(ClientProtocol.U_GHOST_DAMAGE_RANK_LIST, builder);
        }
    }
}
